﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using Word = Microsoft.Office.Interop.Word;

namespace WordTemplate2.Pannel
{
    public partial class UpdateIndex : Form
    {
        public UpdateIndex()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GenerateTOC((int)numericUpDown1.Value);

            Globals.ThisDocument.MoveBookmarkStart("mbookmark1");
            this.Visible = false;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            this.Close();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if (numericUpDown1.Value > 6)
            {
                numericUpDown1.Value = 6;
            }
            if (numericUpDown1.Value < 2)
            {
                numericUpDown1.Value = 2;
            }
            switch ((int)numericUpDown1.Value)
            {
                case 2:
                    {
                        label4.Visible = false;
                        label5.Visible = false;
                        label6.Visible = false;
                        label7.Visible = false;
                    }
                    break;
                case 3:
                    {
                        label4.Visible = true;
                        label5.Visible = false;
                        label6.Visible = false;
                        label7.Visible = false;
                    }
                    break;
                case 4:
                    {
                        label4.Visible = true;
                        label5.Visible = true;
                        label6.Visible = false;
                        label7.Visible = false;
                    }
                    break;
                case 5:
                    {
                        label4.Visible = true;
                        label5.Visible = true;
                        label6.Visible = true;
                        label7.Visible = false;
                    }
                    break;
                case 6:
                    {
                        label4.Visible = true;
                        label5.Visible = true;
                        label6.Visible = true;
                        label7.Visible = true;
                    }
                    break;
            }

        }

        public void GenerateTOC(int lower_level)
        {
            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object character = Word.WdUnits.wdCharacter;
            object line = Word.WdUnits.wdLine;
            object count = 1;

            object varFalseValue = false;
            object varTrueValue = true;
            object varMissing = System.Type.Missing;

            if (!Globals.ThisDocument.CheckBookmarkExist("mbookmark1"))
            {
                Globals.ThisDocument.MoveBookmarkStart("mbookmark2");
                object r_temp = Globals.ThisDocument.Application.Selection.Range;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("temp_range", ref r_temp);

                object pagebreak = Word.WdBreakType.wdPageBreak;
                Globals.ThisDocument.Application.Selection.InsertBreak(ref pagebreak);

                Word.Range r = Globals.ThisDocument.GetBookmarkRange("mbookmark2");
                object count1 = r.End - r.Start - 1;
                Globals.ThisDocument.Application.Selection.MoveRight(ref character, ref count1, ref extend);

                r_temp = Globals.ThisDocument.Application.Selection.Range;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("mbookmark2", ref r_temp);
                Globals.ThisDocument.MoveBookmarkStart("temp_range");

                Globals.ThisDocument.Application.Selection.TypeText(" ");
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref count, ref extend);
                r_temp = Globals.ThisDocument.Application.Selection.Range;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("mbookmark1", ref r_temp);
                Globals.ThisDocument.RemoveBookmark("temp_range");
            }
            else
            {
                Globals.ThisDocument.MoveBookmarkStart("mbookmark1");
                Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark1", "");
                Globals.ThisDocument.Application.Selection.TypeText(" ");
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref count, ref extend);
                object r_temp = Globals.ThisDocument.Application.Selection.Range;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("mbookmark1", ref r_temp);

            }


            Globals.ThisDocument.MoveBookmarkStart("mbookmark1");
            object str = (object)"标准文件_标准正文";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref str);
            Globals.ThisDocument.Application.Selection.TypeText("目  次");
            str = (object)"标准文件_目录标题";
            Globals.ThisDocument.Application.Selection.ParagraphFormat.set_Style(ref str);
            Globals.ThisDocument.Application.Selection.TypeParagraph();
            Globals.ThisDocument.Application.Selection.TypeParagraph();

            object upperlevel = 1;
            object lowerlevel = lower_level;

            Word.TableOfContents toc = Globals.ThisDocument.Application.ActiveDocument.TablesOfContents.Add(Globals.ThisDocument.Application.Selection.Range,
                                                                                ref varTrueValue, ref upperlevel, ref lowerlevel, ref varMissing, ref varMissing,
                                                                                ref varTrueValue, ref varTrueValue, ref varMissing, ref varTrueValue, ref varTrueValue, ref varTrueValue);
            toc.TabLeader = Word.WdTabLeader.wdTabLeaderDots;
        }

    }
}
